import {assign} from "min-dash";

export default function PaletteProvider(
    palette,
    create,
    elementFactory,
    handTool,
    lassoTool,
    spaceTool,
    globalConnect,
    translate,
) {
    this.create = create;
    this.elementFactory = elementFactory;
    this.handTool = handTool;
    this.lassoTool = lassoTool;
    this.spaceTool = spaceTool;
    this.globalConnect = globalConnect;
    this.translate = translate;

    palette.registerProvider(this);
}

PaletteProvider.$inject = [
    "palette",
    "create",
    "elementFactory",
    "handTool",
    "lassoTool",
    "spaceTool",
    "globalConnect",
    "translate",
];


PaletteProvider.prototype.getPaletteEntries = function () {
    const {
        create,
        elementFactory,
        handTool,
        lassoTool,
        spaceTool,
        globalConnect,
        translate,
    } = this;

    function createAction(type, group, className, title, options) {
        function createListener(event) {
            var shape = elementFactory.createShape(assign({type: type}, options));

            if (options) {
                shape.businessObject.di.isExpanded = options.isExpanded;
            }

            create.start(event, shape);
        }

        var shortType = type.replace(/^bpmn:/, "");

        return {
            group: group,
            className: className,
            title: title || translate("Create {type}", {type: shortType}),
            action: {
                dragstart: createListener,
                click: createListener
            }
        };
    }

    function createSubprocess(event) {
        var subProcess = elementFactory.createShape({
            type: 'bpmn:SubProcess',
            x: 0,
            y: 0,
            isExpanded: true
        });

        var startEvent = elementFactory.createShape({
            type: 'bpmn:StartEvent',
            x: 40,
            y: 82,
            parent: subProcess
        });

        create.start(event, [subProcess, startEvent], {
            hints: {
                autoSelect: [startEvent]
            }
        });
    }

    function createParticipant(event) {
        create.start(event, elementFactory.createParticipantShape());
    }

    return {
        "hand-tool": {
            group: "tools",
            className: "bpmn-icon-hand-tool",
            title: translate("Activate the hand tool"),
            action: {
                click: function (event) {
                    handTool.activateHand(event);
                }
            }
        },
        "lasso-tool": {
            group: "tools",
            className: "bpmn-icon-lasso-tool",
            title: translate("Activate the lasso tool"),
            action: {
                click: function (event) {
                    lassoTool.activateSelection(event);
                }
            }
        },
        "space-tool": {
            group: "tools",
            className: "bpmn-icon-space-tool",
            title: translate("Activate the create/remove space tool"),
            action: {
                click: function (event) {
                    spaceTool.activateSelection(event);
                }
            }
        },
        "global-connect-tool": {
            group: "tools",
            className: "bpmn-icon-connection-multi",
            title: translate("Activate the global connect tool"),
            action: {
                click: function (event) {
                    globalConnect.toggle(event);
                }
            }
        },
        "tool-separator": {
            group: "tools",
            separator: true
        },

        "create.start-event": createAction(
            "bpmn:StartEvent",
            "event",
            "bpmn-icon-start-event-none",
            "开始节点"
        ),
        "create.intermediate-catch-event": createAction(
            "bpmn:IntermediateThrowEvent",
            "event",
            "bpmn-icon-intermediate-event-none",
            "中间/边界事件"
        ),
        "create.end-event": createAction(
            "bpmn:EndEvent",
            "event",
            "bpmn-icon-end-event-none",
            "结束节点"
        ),
        "create.exclusive-gateway": createAction(
            "bpmn:ExclusiveGateway",
            "gateway",
            "bpmn-icon-gateway-xor",
            "排他网关"
        ),
        "create.parallel-gateway": createAction(
            "bpmn:ParallelGateway",
            "gateway",
            "bpmn-icon-gateway-parallel",
            "并行网关"
        ),
        "create.user-task": createAction(
            "bpmn:UserTask",
            "activity",
            "bpmn-icon-user-task",
            "用户任务"
        ),
        'create.subprocess-expanded': {
            group: 'activity',
            className: 'bpmn-icon-subprocess-expanded',
            title: translate('Create expanded SubProcess'),
            action: {
                dragstart: createSubprocess,
                click: createSubprocess
            }
        },
        'create.participant-expanded': {
            group: 'collaboration',
            className: 'bpmn-icon-participant',
            title: translate('Create Pool/Participant'),
            action: {
                dragstart: createParticipant,
                click: createParticipant
            }
        },
        "create.group": createAction(
            "bpmn:Group",
            "collaboration",
            "bpmn-icon-group",
            "组"
        ),
        /*create: {
            group: "create",
            html: `<div class="entry"> ${createIcon}</div>`,
            title: translate("Create element"),
            action: {
                click: function(event) {
                    const position = getPosition(event);
                    const rootElement = canvas.getRootElement();

                    popupMenu.open(rootElement, "bpmn-create", position, {
                        title: translate("Create element"),
                        width: 300,
                        search: true
                    });
                }
            }
        }*/
    };
};
